#include "kernel_operator.h"
using namespace AscendC;

constexpr int32_t BUFFER_NUM = 2;

class KernelAsinhFloat
{
    using T = float;

public:
    __aicore__ inline KernelAsinhFloat() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y,
                                uint32_t coreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t tailDataNum, TPipe *pipeIn)
    {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->coreDataNum = coreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = tailDataNum;

        xGm.SetGlobalBuffer((__gm__ T *)x, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ T *)y, this->coreDataNum);
        pipe = pipeIn;

        pipe->InitBuffer(inQueueX, BUFFER_NUM, this->tileDataNum * sizeof(T));
        pipe->InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(T));

        pipe->InitBuffer(tmpBuffer, this->tileDataNum * sizeof(uint8_t));
    }
    __aicore__ inline void Process()
    {
        uint32_t loopCount = this->tileNum;
        // 每次处理的数据量，最后一次是 使用尾块
        this->processDataNum = this->tileDataNum;
        for (uint32_t i = 0; i < loopCount; i++)
        {
            if (i == this->tileNum - 1)
            {
                this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(uint32_t progress)
    {
        LocalTensor<T> xLocal = inQueueX.AllocTensor<T>();
        DataCopy(xLocal, xGm[progress * this->tileDataNum], this->processDataNum);
        inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(uint32_t progress)
    {
        LocalTensor<T> xLocal = inQueueX.DeQue<T>();
        LocalTensor<T> yLocal = outQueueY.AllocTensor<T>();

        auto selMask = tmpBuffer.Get<uint8_t>();

        CompareScalar(selMask, xLocal, static_cast<T>(0), AscendC::CMPMODE::GE, (this->processDataNum + 63) / 64 * 64);
        Abs(xLocal, xLocal, this->processDataNum);

        Mul(yLocal, xLocal, xLocal, this->processDataNum);
        Adds(yLocal, yLocal, static_cast<T>(1), this->processDataNum);
        Sqrt(yLocal, yLocal, this->processDataNum);
        Add(yLocal, xLocal, yLocal, this->processDataNum);
        Ln(yLocal, yLocal, this->processDataNum);

        Muls(xLocal, yLocal, static_cast<T>(-1), this->processDataNum);
        Select(yLocal, selMask, yLocal, xLocal, SELMODE::VSEL_TENSOR_TENSOR_MODE, this->processDataNum);

        outQueueY.EnQue(yLocal);
        inQueueX.FreeTensor(xLocal);
    }
    __aicore__ inline void CopyOut(uint32_t progress)
    {
        LocalTensor<T> yLocal = outQueueY.DeQue<T>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe *pipe;
    TQue<QuePosition::VECIN, 1> inQueueX;
    TQue<QuePosition::VECOUT, 1> outQueueY;
    TBuf<QuePosition::VECCALC> tmpBuffer;

    GlobalTensor<T> xGm;
    GlobalTensor<T> yGm;

    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

class KernelAsinhHalf
{
    using T = half;
    using Tfp32 = float;

public:
    __aicore__ inline KernelAsinhHalf() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y,
                                uint32_t coreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t tailDataNum, TPipe *pipeIn)
    {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->coreDataNum = coreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = tailDataNum;

        xGm.SetGlobalBuffer((__gm__ T *)x, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ T *)y, this->coreDataNum);
        pipe = pipeIn;

        pipe->InitBuffer(inQueueX, BUFFER_NUM, this->tileDataNum * sizeof(T));
        pipe->InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(T));

        pipe->InitBuffer(tmpBuffer, 9 * this->tileDataNum * sizeof(uint8_t));
    }
    __aicore__ inline void Process()
    {
        uint32_t loopCount = this->tileNum;
        // 每次处理的数据量，最后一次是 使用尾块
        this->processDataNum = this->tileDataNum;
        for (uint32_t i = 0; i < loopCount; i++)
        {
            if (i == this->tileNum - 1)
            {
                this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(uint32_t progress)
    {
        LocalTensor<T> xLocal = inQueueX.AllocTensor<T>();
        DataCopy(xLocal, xGm[progress * this->tileDataNum], this->processDataNum);
        inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(uint32_t progress)
    {
        LocalTensor<T> xLocal = inQueueX.DeQue<T>();
        LocalTensor<T> yLocal = outQueueY.AllocTensor<T>();

        auto xBuf = tmpBuffer.Get<Tfp32>(this->processDataNum);
        auto yBuf = tmpBuffer.GetWithOffset<Tfp32>(this->processDataNum, this->processDataNum * sizeof(Tfp32));
        auto selMask = tmpBuffer.GetWithOffset<uint8_t>(this->processDataNum, 2 * this->processDataNum * sizeof(Tfp32));

        Cast(xBuf, xLocal, RoundMode::CAST_NONE, this->processDataNum);
        CompareScalar(selMask, xBuf, static_cast<Tfp32>(0), AscendC::CMPMODE::GE, (this->processDataNum + 63) / 64 * 64);

        Abs(xBuf, xBuf, this->processDataNum);

        Mul(yBuf, xBuf, xBuf, this->processDataNum);
        Adds(yBuf, yBuf, static_cast<Tfp32>(1), this->processDataNum);
        Sqrt(yBuf, yBuf, this->processDataNum);
        Add(yBuf, xBuf, yBuf, this->processDataNum);
        Ln(yBuf, yBuf, this->processDataNum);

        Muls(xBuf, yBuf, static_cast<Tfp32>(-1), this->processDataNum);
        Select(yBuf, selMask, yBuf, xBuf, SELMODE::VSEL_TENSOR_TENSOR_MODE, this->processDataNum);

        Cast(yLocal, yBuf, RoundMode::CAST_NONE, this->processDataNum);

        outQueueY.EnQue(yLocal);
        inQueueX.FreeTensor(xLocal);
    }
    __aicore__ inline void CopyOut(uint32_t progress)
    {
        LocalTensor<T> yLocal = outQueueY.DeQue<T>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe *pipe;
    TQue<QuePosition::VECIN, 1> inQueueX;
    TQue<QuePosition::VECOUT, 1> outQueueY;
    TBuf<QuePosition::VECCALC> tmpBuffer;

    GlobalTensor<T> xGm;
    GlobalTensor<T> yGm;

    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

extern "C" __global__ __aicore__ void asinh(GM_ADDR x, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling)
{
    TPipe pipe;
    GET_TILING_DATA(tiling_data, tiling);
    if (TILING_KEY_IS(1))
    {
        KernelAsinhFloat op;
        op.Init(x, y,
                tiling_data.coreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.tailDataNum, &pipe);
        op.Process();
    }
    else if (TILING_KEY_IS(2))
    {
        KernelAsinhHalf op;
        op.Init(x, y,
                tiling_data.coreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.tailDataNum, &pipe);
        op.Process();
    }
}